package com.sky.business.sys.handler.endpoint;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.business.sys.domain.SystemConfig;
import com.sky.business.sys.handler.MqttEventHandler;
import com.sky.business.sys.service.SystemConfigService;
import com.sky.business.sys.util.MQTTClientUtils;
import com.sky.common.config.CacheConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.SimpleTimeZone;

/**
 * 终端请求转发处理器
 */
@Slf4j
@Component
public class ForwardToCmccEventHandler implements MqttEventHandler {

    @Autowired
    private SystemConfigService systemConfigService;

    public static ObjectMapper objectMapper = new ObjectMapper().setTimeZone(SimpleTimeZone.getTimeZone("GMT+8"));


    @Override
    public boolean match(String topic, String msg,String event) {
        return  "SetHome".equals(event)  || "MoveHome".equals(event)  ||
                    "AbsMoveByAngle".equals(event) || "AbsMove".equals(event) ||
                    "IsDetect".equals(event) ||  "IsTrack".equals(event) || "TargetRelease".equals(event) ||
                    "DetectToggle".equals(event) || "TrackToggle".equals(event) ||
                    "AdjustDeviceInfo".equals(event)   || "EndTracking".equals(event) ;
    }

    @Override
    public void handle(String topic, String msg) throws Exception{
        SystemConfig systemConfig = systemConfigService.getSystemConfig();
        MQTTClientUtils.publish(MQTTClientUtils.CMCC_MQTT_CLIENT,  systemConfig.getCmccReportingTopic(), CacheConstants.DEFAULT_QOS, msg);
    }


}
